﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Giaiphongmatbang.DataModel;

namespace Giaiphongmatbang.BLL
{
    public class ProjectsRepository
    {
        private QLGPMBEntities data = new QLGPMBEntities();

        public IEnumerable<Project> GetAll(int pageSize, int pageNumber, ref int totalRow)
        {
            var results = from p in data.Projects
                          select p;
            totalRow = results.Count();
            return results.OrderByDescending(p=>p.ProjectID).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }

        //Những dự án đã Public
        public IEnumerable<Project> GetIsPublic(int pageSize, int pageNumber, ref int totalRow)
        {
            var results = data.Projects.Where(p => p.isPublic);
            totalRow = results.Count();
            return results.OrderByDescending(p => p.ProjectID).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }
        //Những dự án đã hoàn thành
        public IEnumerable<Project> GetProjectCompleted(int pageSize, int pageNumber, ref int totalRow)
        {
            var results = data.Projects.Where(p => p.isCompleted);
            totalRow = results.Count();
            return results.OrderByDescending(p => p.ProjectID).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }

        //Những dự án không public
        public IEnumerable<Project> GetnotIsPublic(int pageSize, int pageNumber, ref int totalRow)
        {
            var results = data.Projects.Where(p => !p.isPublic);
            totalRow = results.Count();
            return results.OrderByDescending(p => p.ProjectID).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }
  
        //Những dự án chưa hoàn thành
        public IEnumerable<Project> GetProjectnotIsCompleted(int pageSize, int pageNumber, ref int totalRow)
        {
            var results = data.Projects.Where(p => !p.isCompleted);
            totalRow = results.Count();
            return results.OrderByDescending(p => p.ProjectID).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }

        //Danh sách các dự án có trong một xã
        public IEnumerable<Project> GetProjectByVillageID(int villageID, int pageSize, int pageNumber, ref int totalRow)
        {
            var result = (from p in data.Projects
                         join i in data.Inventories on p.ProjectID equals i.ProjectID
                         join h in data.HouseHolds on i.CMND equals h.CMND
                         where h.Village.VillageID == villageID
                         select p).Distinct();
            
            totalRow = result.Count();
            return result.OrderByDescending(p => p.ProjectID).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }

        public List<Project> GetProjectByVillageIDCount(int villageID)
        {
            var result = (from p in data.Projects
                          join i in data.Inventories on p.ProjectID equals i.ProjectID
                          join h in data.HouseHolds on i.CMND equals h.CMND
                          where h.Village.VillageID == villageID
                          select p).Distinct();
            return result.ToList();
        }

        public Project GetByID(int ID)
        {
            var result = from p in data.Projects
                         where p.ProjectID == ID
                         select p;
            return result.FirstOrDefault();
        }


        public void Insert(Project p)
        {
            data.AddToProjects(p);
        }

        public void DeleteByID(int id)
        {
            var obj = GetByID(id);
            if (obj != null)
            {
                data.DeleteObject(obj);
            }
        }

        public void Save()
        {
            data.SaveChanges();
        }
    }
}